package client;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class EditItem extends JFrame implements ActionListener
{
	public Item item = null;
	public JButton cancel = null;
	public JButton submit = null;
	public JLabel itemName = null;
	public JRadioButton setNewPrice = null;
	public JTextField newPrice = null;
	public JLabel itemId = null;
	public JLabel itemType = null;
	public JLabel itemPrice = null;
	public JLabel itemDiscount = null;
	public JPanel panel = null;
	public JRadioButton inCatalog = null;
	public JRadioButton notInCatalog = null;
	public JRadioButton setNewSale = null;
	public JRadioButton RemoveSale = null;
	public ButtonGroup btnGroup = null;
	public ButtonGroup saleBtnGroup = null;
	public ImagePanel img = null;
	public byte[] icon = null;
	
	boolean flag = true;
	boolean newPriceflag = false;
	boolean disflag = false;
	
	float oldPrice = -1;
	float oldPriceWithNoDiscount = -1;
	float newPriceWithNoDiscount = -1;
	float nPrice = -1;
	int discount = -1;
	int itemIdForClassItem = -1;
	
	public EditItem(Object[][] obj, int i, ArrayList<Item> list)
	{
		itemIdForClassItem = (Integer)obj[i][4];
		oldPrice = (Float)obj[i][2];
		discount = (Integer)list.get(i).discount;
		if(discount == 1)
			oldPriceWithNoDiscount = (float)(oldPrice/0.7);
		else
			oldPriceWithNoDiscount = oldPrice;
		
		panel = new JPanel();
		panel.setLayout(null);
		img = new ImagePanel(list.get(i).getMyIconArray());
		img.setBounds(300,40,200,200);
		
		this.setTitle("Edit Item");
		this.setPreferredSize(new Dimension(500, 450));
		this.setLocation((int)Main.dim.getWidth()/2 - (int)this.getPreferredSize().getWidth()/2,(int)Main.dim.getHeight()/2 - (int)this.getPreferredSize().getHeight()/2);
		this.setResizable(false);
		
		this.addWindowListener(new WindowAdapter() 
        {public void windowClosing(WindowEvent e){cancel.doClick();}});
		
  	  	itemName = new JLabel("Item Name : "+obj[i][0]+"");
  	  	itemName.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,19));
  	  	itemName.setBounds(10, 5, 300, 30);
	  	panel.add(itemName);
  	  	
	  	itemId = new JLabel("Item Id : "+obj[i][4]+"");
	  	itemId.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,19));
	  	itemId.setBounds(10, 35, 300, 30);
	  	panel.add(itemId);
	  	
	  	itemType = new JLabel("Item Type : "+obj[i][3]+"");
	  	itemType.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,19));
	  	itemType.setBounds(10, 65, 300, 30);
	  	panel.add(itemType);
	  	
	  	itemPrice = new JLabel("Price On Sale : "+obj[i][2]+"");
	  	itemPrice.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,19));
	  	itemPrice.setBounds(10, 95, 300, 30);
	  	panel.add(itemPrice);
	  	
	  	if(discount == 1)
	  		itemDiscount = new JLabel("Item On Special Sale: Yes");
	  	if(discount == 0)
	  		itemDiscount = new JLabel("Item On Special Sale: No");
	  	
	  	itemDiscount.setFont(new Font (Font.SANS_SERIF ,Font.ITALIC,19));
	  	itemDiscount.setBounds(10, 125, 300, 30);
	  	panel.add(itemDiscount);
	  	
  	  	inCatalog = new JRadioButton("Item In Catalog");
	  	inCatalog.setBounds(40, 190, 150, 20);
	  	inCatalog.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
	  	inCatalog.addActionListener(this);
		panel.add(inCatalog);
		
		notInCatalog = new JRadioButton("Item Not In Catalog");
		notInCatalog.setBounds(260, 190, 200, 20);
		notInCatalog.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		notInCatalog.addActionListener(this);
		panel.add(notInCatalog);
		
		setNewSale = new JRadioButton("Item On Special Sale");
		setNewSale.setBounds(40, 235, 210, 20);
		setNewSale.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		setNewSale.addActionListener(this);
		panel.add(setNewSale);
		
		RemoveSale = new JRadioButton("Set Item Original Price");
		RemoveSale.setBounds(260, 235, 230, 20);
		RemoveSale.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		RemoveSale.addActionListener(this);
		panel.add(RemoveSale);
		
		saleBtnGroup = new ButtonGroup();
		saleBtnGroup.add(setNewSale);
		saleBtnGroup.add(RemoveSale);
		
		setNewPrice = new JRadioButton("Set New Price Before Sale:");
		setNewPrice.setBounds(60, 290, 240, 20);
		setNewPrice.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		setNewPrice.addActionListener(this);
		panel.add(setNewPrice);
		
		newPrice = new JTextField();
		newPrice.setBounds(325, 290, 95, 25);
		newPrice.setBackground(Color.LIGHT_GRAY);
		newPrice.setBackground(new Color(219,219,219));
		newPrice.setText("New price");
		newPrice.setForeground(Color.BLACK);
		newPrice.setFont(new Font(Font.SANS_SERIF ,Font.PLAIN,17));
		newPrice.setEnabled(false);
		panel.add(newPrice);
		
		btnGroup = new ButtonGroup();
		btnGroup.add(inCatalog);
		btnGroup.add(notInCatalog);
		
		if(discount == 1)
		{
			setNewSale.setSelected(true);
			setNewSale.setEnabled(false);
			RemoveSale.setEnabled(true);
			RemoveSale.setSelected(false);
		}
		else //remove from sale
			{
				setNewSale.setSelected(false);
				setNewSale.setEnabled(true);
				RemoveSale.setSelected(true);
				RemoveSale.setEnabled(false);
			}
		
		if(list.get(i).inCatalog == 1)
		{
			notInCatalog.setSelected(false);
			inCatalog.setSelected(true);
			notInCatalog.setEnabled(true);
			inCatalog.setEnabled(false);
		}
		else
		{
			notInCatalog.setSelected(true);
			inCatalog.setSelected(false);
			notInCatalog.setEnabled(false);
			inCatalog.setEnabled(true);
		}
			  	  	
		cancel = new JButton("Cancel");
		cancel.setBounds(10,350,230,50);
		cancel.addActionListener(this);
		panel.add(cancel);
		
		submit = new JButton("Submit");
		submit.setBounds(250,350,230,50);
		submit.addActionListener(this);
		panel.add(submit);
		
		panel.add(img);
		this.add(panel);
		this.pack();
		this.setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource() == submit)
		{	
			item = new Item(itemIdForClassItem);
			
			if(setNewPrice.isSelected())
			{
				try
				{
					newPriceWithNoDiscount = Float.valueOf(newPrice.getText().trim()).floatValue();
					if(newPriceWithNoDiscount < 0)
						newPriceWithNoDiscount = Float.valueOf("**").floatValue();//will throw an Exception	
				}
			    catch (NumberFormatException nfe)
			    {
			    	JOptionPane.showMessageDialog(null,"Only digits are permitted,\nValues bigger then 0 ","Error",JOptionPane.ERROR_MESSAGE);
			    	flag = false;
			    }
			}
			else
				newPriceWithNoDiscount = oldPriceWithNoDiscount;
			
			if(inCatalog.isSelected())
				item.inCatalog = 1;
			else
				item.inCatalog = 0;
			
			if(setNewSale.isSelected())
			{
				 if(newPriceWithNoDiscount != oldPriceWithNoDiscount)
					 nPrice = (float)(newPriceWithNoDiscount * 0.7);
				 else
					 nPrice = (float)(oldPriceWithNoDiscount * 0.7);
				 item.discount = 1;
			}
			else
			{
				nPrice = newPriceWithNoDiscount;
				item.discount = 0;
			}		
			item.newPrice = nPrice;
			
			System.out.println(item.newPrice);//testing
				
			if(LogIn.usr.getipAdd().equals(""))
				Main.serverIP = "localhost";
			else 
				Main.serverIP = LogIn.usr.getipAdd();
				
			Main.clie = new ClientConsole(Main.serverIP, Main.DEFAULT_PORT);
			Main.clie.accept(item);
				
			while(Main.clie.flag == false);
			Main.clie.flag = false;
				
			int result = (Integer)Main.clie.getResult();
				
			if(result == 1)
			{
				JOptionPane.showMessageDialog(null,"Catalog was successfully updated");
				this.dispose();
				Main.main.catalogManag.dispose();
				Main.main.catalogManaging.doClick();
			}	
			else
				JOptionPane.showMessageDialog(null,"Error occur while updating catalog/nPlease try again ","Error",JOptionPane.ERROR_MESSAGE);
		}
		
		if(e.getSource() == RemoveSale)
		{			
			JOptionPane.showMessageDialog(null,"After submit this item price will be set to his original price");
			RemoveSale.setEnabled(false);
			setNewSale.setEnabled(true);
		}
		if(e.getSource() == setNewSale)
		{
			JOptionPane.showMessageDialog(null,"After submit this item price will be set to a special price: 30% off");
			RemoveSale.setEnabled(true);
			setNewSale.setEnabled(false);
		}
		
		if(e.getSource() == cancel)
		{
			Main.main.catalogManag.setEnabled(true);
			Main.main.catalogManag.setVisible(true);
			this.dispose();
		}
		if(e.getSource() == inCatalog)
		{
			JOptionPane.showMessageDialog(null,"After submit this item will be added to catalog");
			inCatalog.setEnabled(false);
			notInCatalog.setEnabled(true);
		}
		
		if(e.getSource() == notInCatalog)
		{
			JOptionPane.showMessageDialog(null,"After submit this item will be remove from catalog");
			inCatalog.setEnabled(true);
			notInCatalog.setEnabled(false);
		}
		
		if(e.getSource() == setNewPrice)
		{
			if(setNewPrice.isSelected())
			{
				newPrice.setEnabled(true);
				newPrice.setText("");
				newPrice.setBackground(Color.WHITE);
			}
			else 
				if(!setNewPrice.isSelected())
				{
					newPrice.setText("New price");
					newPrice.setForeground(Color.BLACK);
					newPrice.setBackground(new Color(219,219,219));
					newPrice.setEnabled(false);
				}
		}
	}

	public class ImagePanel extends JPanel 
	{
	    private Image img;

	    public ImagePanel(byte[] img) 
	    {
	        this(new ImageIcon(img).getImage());
	    }

	    public ImagePanel(Image img) 
	    {
	        this.img = img;
	        Dimension size = new Dimension(img.getWidth(null),img.getHeight(null));
	        setPreferredSize(size);
	        setMinimumSize(size);
	        setMaximumSize(size);
	        setSize(size);
	        setLayout(null);
	    }

	    public void paintComponent(Graphics g) {
	        g.drawImage(img,0,0,null);
	    }

	}
}
